package com.douglas.managesystem.controller.filed;

import com.douglas.managesystem.common.FileConfiguration;
import com.douglas.managesystem.common.ResponseResult;
import com.douglas.managesystem.common.UUIDUtils;
import com.douglas.managesystem.entity.SystemConfig;
import com.douglas.managesystem.service.system.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 上传文件入口
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-10-29 15:31
 **/
@Controller
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private FileConfiguration fileConfiguration;
    @Autowired
    private SystemService systemService;

    @RequestMapping("/uploadFile")
    @ResponseBody
    public ResponseResult upload(@RequestParam("file")MultipartFile file){
        System.out.println(file.toString());
        // 1.创建返回对象
        ResponseResult responseResult = new ResponseResult();
        // 2.创建一个json对象 用于存储返回数据信息
//        JSONObject result = new JSONObject();
        Map<String,Object> result = new HashMap<>();
        try{
            // 3.判断文件是否为空
            if(file.isEmpty()){
                responseResult.setInfo("上传文件不存在！！！");
                responseResult.setSuccess(false);
                return responseResult;
            }
            // 4.定义文件存放的路径
            String rootPath = fileConfiguration.getResourceDir();
            // 5.获取当前系统日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String currentDate = sdf.format(new Date());
            // 6.查询系统配置表，获取上传文件夹名称
            SystemConfig systemConfig = systemService.getSystem();
            String uploadDir = systemConfig.getUploaddir();
            // 7.构建文件存放的全目录
            File directory = new File(rootPath + File.separator+ uploadDir + File.separator + currentDate);
            // 8.判断这个目录是否存在 不存在则创建
            if(!directory.exists()){
                directory.mkdirs();
            }
            // 9.生成新的文件名称
            String newFileName = UUIDUtils.getPrimaryKey() + file.getOriginalFilename()
                                .substring(file.getOriginalFilename().lastIndexOf("."));
            // 10.获取创建好的绝对路径
            String absolutePath = directory.getAbsolutePath();
            // 11.构建上传路径
            File uploadpath = new File(absolutePath + File.separator + newFileName);
            // 12.上传文件
            file.transferTo(uploadpath);
            // 13.封装返回信息
            result.put("filepath", currentDate+File.separator + newFileName);
            result.put("name", newFileName);
            result.put("originalFilename", file.getOriginalFilename());
            result.put("filesize", file.getSize());
            result.put("filetype", file.getContentType());
            result.put("url", "http://localhost:8866/" + File.separator + uploadDir + File.separator + currentDate + File.separator + newFileName);
            responseResult.setSuccess(true);
            responseResult.setInfo("上传文件成功！！！");
            responseResult.setData(result);
            System.out.println(responseResult);
            return responseResult;
        }catch (Exception e){
            responseResult.setInfo("上传文件失败！！！");
            responseResult.setSuccess(false);
            return responseResult;
        }

    }
}
